package com.spc.car.web.log.module;

import com.spc.car.common.utils.spring.SpringUtils;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.entity.log.LogProjectOperation;
import com.spc.car.data.entity.project.ProjectTenderingSection;
import com.spc.car.data.service.project.ProjectTenderingSectionService;
import com.spc.car.web.log.ProjectAction;
import com.spc.car.web.log.annotation.LogProjectAction;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;

public class LogProjectTenderingSection extends BaseLogModule<LogProjectOperation, LogProjectAction> {
    public ProjectTenderingSectionService sectionService;

    public LogProjectTenderingSection() {
        this.sectionService = SpringUtils.getBean(ProjectTenderingSectionService.class);
    }

    public LogProjectOperation toLogRecord(LogProjectAction logProjectAction,
                                           Map<String, Object> paramsMap,
                                           AjaxResult result) {
        LogProjectOperation logProjectOperation = new LogProjectOperation();

        ProjectAction[] projectActions = logProjectAction.action();
        ProjectTenderingSection section = null;
        if (StringUtils.isNotBlank(logProjectAction.idForPost())
                && Objects.nonNull(paramsMap.get(logProjectAction.idForPost()))) {
            section = sectionService.getById((Long)paramsMap.get(logProjectAction.idForPost()));
        }

        for (ProjectAction projectAction: projectActions) {
            if (projectAction.equals(ProjectAction.ADD_SECTION)
                    && null == section
                    && StringUtils.isNotBlank(logProjectAction.idForReturn())
                    && Objects.nonNull(result.getByKey(logProjectAction.idForReturn()))) {
                Object idObj =  result.getByKey(logProjectAction.idForReturn());
                if (idObj instanceof String) {
                    logProjectOperation.setProjectTenderingSectionCode((String)idObj);
                    logProjectOperation.setProjectTenderingCode((String)paramsMap.get(""));
                    logProjectOperation.setRemark(projectAction.getName()
                            + "[" + paramsMap.get("name") + "]");
                    break;
                }
            } else if (projectAction.equals(ProjectAction.UPDATE_SECTION) && null != section) {
                logProjectOperation.setProjectTenderingSectionCode(section.getSectionCode());
                logProjectOperation.setProjectTenderingCode(section.getProjectTenderingCode());
                logProjectOperation.setRemark(projectAction.getName()
                        + "[" + paramsMap.get("name") + "]");
                break;
            } else if (projectAction.equals(ProjectAction.DISABLE_SECTION) && null != section) {
                logProjectOperation.setProjectTenderingSectionCode(section.getSectionCode());
                logProjectOperation.setProjectTenderingCode(section.getProjectTenderingCode());
                logProjectOperation.setRemark(projectAction.getName()
                        + "[" + section.getSectionName() + "]");
                break;
            }
        }

        return logProjectOperation;
    }
}
